package com.dycx.cms.converter;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dycx.cms.dto.ArticleDTO;
import com.dycx.cms.entity.ArticleDO;
import com.dycx.cms.vo.ArticleVO;
import com.dycx.common.vendor.oss.AliyunOss;
import lombok.Data;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * (Article)实体转换类
 *
 * @author makejava
 * @since 2024-08-12 03:04:00
 */


@Data
public class ArticleConverter implements Serializable {
    public static ArticleDO dtoToEntity(ArticleDTO dto) {
        if (dto == null) return null;
        ArticleDO entity = BeanUtil.toBean(dto, ArticleDO.class);
        entity.setThumbnail(AliyunOss.delDomain(dto.getThumbnail()));
        entity.setVideo(AliyunOss.delDomain(dto.getVideo()));
        return entity;
    }

    public static ArticleVO entityToVo(ArticleDO entity) {
        if (entity == null) return null;
        ArticleVO vo = BeanUtil.toBean(entity, ArticleVO.class);
        vo.setThumbnail(AliyunOss.addDomain(vo.getThumbnail()));
        vo.setVideo(AliyunOss.addDomain(vo.getVideo()));
        return vo;
    }

    public static List<ArticleVO> entityToVoList(List<ArticleDO> entityList) {
        List<ArticleVO> voList = new ArrayList<>();
        for (ArticleDO entity : entityList) {
            voList.add(entityToVo(entity));
        }
        return voList;
    }

    public static Page<ArticleVO> entityToVoPage(Page<ArticleDO> page) {

        List<ArticleVO> newRecords = new ArrayList<>();

        List<ArticleDO> records = page.getRecords();
        for (ArticleDO entity : records) {
            newRecords.add(entityToVo(entity));
        }

        Page<ArticleVO> voPage = new Page<>();
        voPage.setRecords(newRecords);
        voPage.setPages(page.getPages());
        voPage.setTotal(page.getTotal());
        voPage.setSize(page.getSize());
        voPage.setCurrent(page.getCurrent());

        return voPage;
    }

}
